package com.ljc.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljc.dao.ExpertDao;
import com.ljc.dto.Result;
import com.ljc.entity.Administrator;
import com.ljc.entity.Expert;
import com.ljc.entity.ExpertGradeCount;
import com.ljc.service.ExpertService;
import com.ljc.util.JWTUtil;
import com.ljc.vo.ExpertSearch;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("expert")
@Slf4j
@CrossOrigin
public class ExpertController {

    @Autowired
    private ExpertService expertService;

    @RequestMapping("search")
    public Result searchExpert(HttpServletRequest request, @RequestBody ExpertSearch expertSearch) {
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        expertSearch.setDeanId(Integer.valueOf(id));
        List<Expert> experts;
        if (expertSearch.getPageNum() != -1) { //启动分页
            PageHelper.startPage(expertSearch.getPageNum(), expertSearch.getPageSize());
            experts = expertService.searchExpert(expertSearch);
            PageInfo<Expert> expertPageInfo = new PageInfo<>(experts);
            result.setStatus(1).setMsg("查询成功").setData(expertPageInfo);
            return result;
        } else {   //不分页
            experts = expertService.searchExpert(expertSearch);
            result.setStatus(1).setMsg("查询成功").setData(experts);
            return result;
        }


    }

    @RequestMapping("getExpertGradeCount")
    public Result getExpertGradeCount(HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        List<ExpertGradeCount> expertGradeCount = expertService.getExpertGradeCount(Integer.valueOf(id));
        result.setStatus(1).setMsg("获取专家等级统计成功").setData(expertGradeCount);
        return result;
    }

    @RequestMapping("addExpert")
    public Result addExpert(HttpServletRequest request, @RequestBody Expert expert) {
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        expert.setDeanId(Integer.valueOf(id));
        try {
            expertService.addExpert(expert);
            result.setStatus(1).setMsg("增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(0).setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("getExpertById")
    public Result getExpertById(@RequestParam int id) {
        Result result = new Result();
        Expert expert = expertService.findExpertById(id);
        result.setMsg("获取成功").setStatus(1).setData(expert);
        return result;
    }

    @RequestMapping("updateExpert")
    public Result updateExpert(@RequestBody Expert expert) {
        Result result = new Result();
        expertService.updateExpert(expert);
        result.setMsg("修改成功").setStatus(1).setData(expert);
        return result;
    }

    @RequestMapping("deleteExpert")
    public Result deleteExpert(@RequestParam int id) {
        Result result = new Result();
        try {
            expertService.deleteExpert(id);
            result.setStatus(1).setMsg("删除成功");
        } catch (Exception e) {
            result.setStatus(0).setMsg("删除失败请稍后再试");
        }

        return result;

    }
    @RequestMapping("getActiveExpert")
    public Result getActiveExpert(HttpServletRequest request){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        String id = JWTUtil.getTokenInfo(token).getClaim("id").asString();
        List<Expert> activeExpert = expertService.getActiveExpert(Integer.valueOf(id));
        result.setStatus(1).setMsg("获取成功").setData(activeExpert);
        return result;
    }
    @RequestMapping("login")
    public Result login(@RequestBody Expert expert){
        Result result = new Result();
        try {
            Expert expertDB = expertService.login(expert);
            result.setStatus(1).setMsg("登录成功");
            HashMap<String, String> map = new HashMap<>();
            map.put("id", String.valueOf(expertDB.getId()));
            map.put("name",expertDB.getName());
            String token = JWTUtil.getToken(map);
            result.setData(token);

        }catch (Exception e){
            result.setStatus(0).setMsg(e.getMessage());
        }
        return result;
    }
    @RequestMapping("getInfo")
    public Result getInfo(HttpServletRequest request){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        String id = JWTUtil.getTokenInfo(token).getClaim("id").asString();
        Expert expertById = expertService.findExpertById(Integer.valueOf(id));
        result.setStatus(1).setMsg("获取专家个人信息成功").setData(expertById);
        return result;
    }
    @RequestMapping("adds")
    public Result adds(HttpServletRequest request,@RequestBody List<Expert> experts){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        int count=0;
        for (Expert expert : experts) {
            expert.setStatus(0);
            expert.setDeanId(Integer.valueOf(id));
            try {
                expertService.addExpert(expert);
                count++;
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if (count>0){
            result.setStatus(1);
        }else{
            result.setStatus(0);
        }
        result.setData(count);
        return result;

    }
}
